<template>
  <div class="repair-record">
    <div>
      <label>{{ $t("route.Maintenance_Management") }}</label>
      <div></div>
      <button>
        <img /><label>{{ $t("route.Equipment_maintenance_record") }}</label>
      </button>
    </div>

    <div>
      <div v-show="!noseCode">
        <el-select
          @change="selectChange1"
          style="margin-left: 10px"
          v-model="query.kind"
          :placeholder="$t('route.Please_select_the_type')"
        >
          <el-option v-for="item in kindList" :key="item" :label="item" :value="item">
          </el-option>
        </el-select>

        <div class="div-handle-right">
          <el-button @click="searchClick">{{ $t("route.search") }}</el-button>
          <input v-model="query.search" :placeholder="$t('route.Head_Model')" />
        </div>
      </div>

      <el-table class="table" :data="repairList" style="width: 100%">
        <el-table-column prop="kind" :label="$t('route.kind')" />
        <el-table-column prop="noseCode" :label="$t('route.Head_Model')" />
        <el-table-column :label="$t('route.location')">
          <template #default="scope">
            {{ scope.row.workshop ? scope.row.workshop : scope.row.store }}
          </template>
        </el-table-column>
        <el-table-column
          prop="problem"
          :label="$t('route.Maintenance_issues')"
          width="200"
        />
        <el-table-column
          prop="nickname"
          width="80"
          :label="$t('route.Repair_applicant')"
        />
        <el-table-column :label="$t('route.Repair_time')">
          <template #default="scope">
            {{ new Date(scope.row.createTime).format("yyyy-MM-dd hh:mm:ss") }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('route.Maintenance_time')">
          <template #default="scope">
            {{
              scope.row.repairTime
                ? new Date(scope.row.repairTime).format("yyyy-MM-dd hh:mm:ss")
                : ""
            }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('route.Waiting_time')">
          <template #default="scope">
            {{ formatWaitDur(scope.row) }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('route.Maintenance_duration')">
          <template #default="scope">
            {{ formatRepairDur(scope.row) }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('route.Total_time')">
          <template #default="scope">
            {{ formatAllDur(scope.row) }}
          </template>
        </el-table-column>
        <el-table-column width="80" :label="$t('route.Processing_results')">
          <template #default="scope">
            <label
              :style="{
                color:
                  formatResult(scope.row.result) == $t('route.normal')
                    ? '#444444'
                    : '#FF0000',
              }"
              >{{ formatResult(scope.row.result) }}</label
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          style="float: right"
          background
          @current-change="handlePageChange"
          :current-page="query.pageNum"
          :page-size="query.pageSize"
          layout="total, prev, pager, next"
          :total="query.total"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import http from "../../js/http/http.js";
import api from "../../js/http/api.js";
import CommonUtils from "../../js/common/common-utils.js";
import Constant from "../../js/common/constant.js";
import i18n from "../../i18n";
export default {
  data() {
    return {
      query: {
        kind: i18n.global.t("route.All_categories"),
        pageNum: 1,
        pageSize: 12,
        total: 0,
        search: null,
      },
      repairList: [],
      kindList: [i18n.global.t("route.All_categories"), ...Constant.DEVICE_KIND],
      noseCode: null,
    };
  },
  methods: {
    formatAllDur(repair) {
      var dur = 0;
      if (repair.finishTime) {
        dur = repair.finishTime - repair.createTime;
      } else {
        dur = new Date().getTime() - repair.createTime;
      }
      return CommonUtils.timeStamp(dur / 1000);
    },

    formatWaitDur(repair) {
      if (repair.repairTime) {
        var dur = repair.repairTime - repair.createTime;
        return CommonUtils.timeStamp(dur / 1000);
      }
    },
    formatRepairDur(repair) {
      if (repair.finishTime) {
        var dur = repair.finishTime - repair.repairTime;
        return CommonUtils.timeStamp(dur / 1000);
      }
    },
    selectChange1() {
      this.query.search = null;
      this.query.pageNum = 1;
      this.getRepairList();
    },

    handlePageChange(pageNum) {
      this.query.pageNum = pageNum;
      this.getRepairList();
    },
    searchClick() {
      this.query.pageNum = 1;
      this.getRepairList();
    },
    getRepairList() {
      http.post(
        api.GET_REPAIR_LIST4,
        {
          adminId: this.$store.state.user.id,
          noseCode: this.noseCode,
          factory: Constant.FACTORY == "全部工厂" ? null : Constant.FACTORY,
          kind:
            this.query.kind == i18n.global.t("route.All_categories")
              ? null
              : this.query.kind,
          state: 0,
          search: this.query.search,
          pageNum: this.query.pageNum,
          pageSize: this.query.pageSize,
        },
        (data) => {
          this.repairList = data.data.repairList;
          this.query.total = data.data.total;
        }
      );
    },
    formatResult(result) {
      //0:没有异常 1:普通维修 2:大修 3报废 4:异常报警
      if (result == 0) {
        return i18n.global.t("route.normal");
      } else if (result == 1) {
        return i18n.global.t("route.General_maintenance");
      } else if (result == 2) {
        return i18n.global.t("route.Overhaul");
      } else if (result == 3) {
        return i18n.global.t("route.Scrap");
      } else if (result == 4) {
        return i18n.global.t("route.Abnormal_alarm");
      }
    },
    getAllKind() {
      http.post(
        api.GET_ALL_KIND,
        {
          adminId: this.$store.state.user.id,
        },
        (data) => {
          this.kindList = [i18n.global.t("route.All_categories"), ...data.data];
        }
      );
    },
  },
  created() {
    this.noseCode = CommonUtils.getParam(this, "noseCode", "setNoseCode");
    this.user = this.$store.state.user;
    this.getAllKind();
    this.getRepairList();
  },

  mounted() {},
};
</script>
<style lang="scss" scoped>
.repair-record {
  color: #444444;
  font-size: 18px;
  padding-right: 20px;
  height: calc(100vh - 100px);
  // overflow: scroll;
  scrollbar-width: none;
  /* firefox */
  -ms-overflow-style: none;
  /* IE 10+ */
  overflow-x: hidden;
  overflow-y: auto;

  > div:nth-of-type(1) {
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;

    > label:nth-of-type(1) {
      font-weight: 600;
    }

    > div:nth-of-type(1) {
      width: 1px;
      background-color: #cccccc;
      height: 15px;
      margin: 0px 20px;
    }

    > button:nth-of-type(1) {
      background-image: url(../../assets/img/customer-1.png);
      background-repeat: no-repeat;
      background-size: 100% 100%;
      padding: 5px 10px;
      border: 0px;
      display: flex;
      color: #ffffff;
      flex-direction: row;
      align-items: center;
      font-weight: 800;

      > img {
        margin-right: 7px;
        width: 15px;
        height: 15px;
        content: url(../../assets/img/customer-2.png);
      }
    }
  }

  > div:nth-of-type(2) {
    display: flex;
    flex-direction: column;
    margin-top: 20px;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 5px;

    > div:nth-of-type(1) {
      display: flex;
      flex-direction: row;
      align-items: center;

      .div-handle-right {
        flex: 1;
        flex-direction: row-reverse;
        text-align: right;
        display: flex;
        align-items: center;

        > input:nth-of-type(1) {
          border: 1px solid #eeeeee;
          font-size: 15px;
          width: 200px;
          padding: 5px 8px;
          outline: none;
        }

        > button {
          color: #a866ff;
          margin-left: 10px;
          padding: 4px 20px;
        }
      }
    }

    .table {
      margin-top: 20px;
    }
  }

  .select {
    width: 120px;
    margin-top: 10px;
  }

  .echart {
    height: 150px;
    width: 100%;
  }

  .input {
    width: 250px;
  }
}
</style>
